package com.ylbh.modules.brand.service.impl;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.ylbh.common.pojo.brand.dto.BrandGoodsDTO;
import com.ylbh.common.pojo.brand.dto.ShoppingGoodsBrandDTO;
import com.ylbh.common.pojo.brand.vo.ShoppingBrandGoodsVO;
import com.ylbh.common.utils.IdWorker;
import com.ylbh.common.utils.OSSClientUtil;
import com.ylbh.common.utils.PageUtils;
import com.ylbh.common.utils.ShiroUtils;
import com.ylbh.modules.brand.entity.BeanVO;
import com.ylbh.modules.brand.entity.ShoppingBrandClass;
import com.ylbh.modules.brand.entity.ShoppingGoodsBrand;
import com.ylbh.modules.brand.mapper.ShoppingBrandGoodsMapper;
import com.ylbh.modules.brand.service.ShoppingBrandGoodsService;
import com.ylbh.modules.goods.service.ShoppingGoodsService;
import org.apache.commons.lang.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.web.multipart.MultipartFile;

import java.util.Arrays;
import java.util.Date;
import java.util.List;

@Service
public class ShoppingBrandGoodsServiceImpl implements ShoppingBrandGoodsService {

    private  final Logger log = LoggerFactory.getLogger(ShoppingBrandGoodsServiceImpl.class);

    @Autowired
    private ShoppingBrandGoodsMapper shoppingBrandGoodsMapper;

    @Autowired
    private ShoppingGoodsService shoppingGoodsService;

    @Override
    public PageUtils queryBrandGoodsList(BrandGoodsDTO brandGoodsDTO) {

        Page<Object> page = new Page<>( brandGoodsDTO.getPage(), brandGoodsDTO.getLimit());
        List<ShoppingBrandGoodsVO> list = shoppingBrandGoodsMapper.selectBrandGoodsList( brandGoodsDTO, page);
        for ( ShoppingBrandGoodsVO vo : list ) {
            String sku = vo.getId();
            if (StringUtils.isNotBlank(sku)){
                vo.setGoodsCount(shoppingGoodsService.getCountByBrandId(sku));
            }
        }
        Page<ShoppingBrandGoodsVO> page1 = new Page<>();
        page1.setRecords(list).setTotal(page.getTotal());
        return new PageUtils(page1);
    }

    @Override
    public void modifyBrandEnableStatusById(BrandGoodsDTO brandGoodsDTO) {
        shoppingBrandGoodsMapper.updateEnableStatusById(brandGoodsDTO);
    }

    @Override
    public String uploadBrand(MultipartFile file) {
        OSSClientUtil ossClient = null;
        try {
            ossClient = new OSSClientUtil();
            String imgUrl = ossClient.uploadImg2Oss(file,"brand");
            if("fail".equals(imgUrl)){
                return  "上传失败";
            }
            return "http://image.yilianbaihui.cn/consumer/brand/" + imgUrl;
        } catch (Exception e) {
            e.printStackTrace();
            return "上传失败";
        }

    }

    @Override
    public void addBrandGoods(ShoppingGoodsBrandDTO shoppingGoodsBrandDTO) {
        ShoppingGoodsBrand brand = new ShoppingGoodsBrand();
        BeanUtils.copyProperties(shoppingGoodsBrandDTO,brand);
        IdWorker idW = new IdWorker();
        Date date = new Date();
        brand.setCreateTime(date);
        brand.setShowState(1);
        brand.setCreateUserId(ShiroUtils.getUserId().toString());
        brand.setUpdateTime(date);
        brand.setUpdateUserId(ShiroUtils.getUserId().toString());
            //添加品牌信息
            shoppingBrandGoodsMapper.insertBrand(brand);
            System.out.println(brand.getId());
            for (BeanVO classList : shoppingGoodsBrandDTO.getClassList()) {
                //插入中间表
                if (null != classList){
                    ShoppingBrandClass shoppingBrandClass = new ShoppingBrandClass();
                    shoppingBrandClass.setId(idW.nextId());
                    shoppingBrandClass.setGoodsBrandId(brand.getId());
                    shoppingBrandClass.setGoodsClassId(classList.getSelectSubClassId());
                    shoppingBrandClass.setCreateTime(date);
                    shoppingBrandGoodsMapper.insertBrandCalss(shoppingBrandClass);
                }
            }
    }

    @Override
    public ShoppingBrandGoodsVO getBrand(String id) {
        //品牌信息
        ShoppingBrandGoodsVO shoppingBrandGoodsVO = shoppingBrandGoodsMapper.selectById(id);
        //关连二级分类
        shoppingBrandGoodsVO.setList(shoppingBrandGoodsMapper.selectBrandClassById(id));
        return shoppingBrandGoodsVO;
    }

    @Override
    public void modifyBrandById(ShoppingGoodsBrandDTO shoppingGoodsBrandDTO) {
        //删除关联表信息
        shoppingBrandGoodsMapper.deleteBrandClass(shoppingGoodsBrandDTO.getId());
        ShoppingGoodsBrand brand = new ShoppingGoodsBrand();
        BeanUtils.copyProperties(shoppingGoodsBrandDTO,brand);
        IdWorker idW = new IdWorker();
        Date date = new Date();
        brand.setShowState(1);
        brand.setUpdateTime(date);
        brand.setUpdateUserId(ShiroUtils.getUserId().toString());
        //便新品牌信息
        shoppingBrandGoodsMapper.updateBrandById(brand);
        for (BeanVO classList : shoppingGoodsBrandDTO.getClassList()) {
            //插入中间表
            if (null != classList){
                ShoppingBrandClass shoppingBrandClass = new ShoppingBrandClass();
                shoppingBrandClass.setId(idW.nextId());
                shoppingBrandClass.setGoodsBrandId(brand.getId());
                shoppingBrandClass.setGoodsClassId(classList.getSelectSubClassId());
                shoppingBrandClass.setCreateTime(date);
                shoppingBrandGoodsMapper.insertBrandCalss(shoppingBrandClass);
            }
        }
    }

    @Override
    public void removeBrandById(String id) {
        shoppingBrandGoodsMapper.deleteBrandById(id);
    }

    @Override
    public void modifyBrandEnableStateByIds(String batchValue, String[] ids) {
        shoppingBrandGoodsMapper.updateBrandEnableStateByIds(batchValue, Arrays.asList(ids));
    }

    @Override
    public List<ShoppingGoodsBrand> queryBrandList(String id) {
        return shoppingBrandGoodsMapper.selectBrandList(id);
    }

    @Override
    public void modifyBrandShowStateById(BrandGoodsDTO brandGoodsDTO) {
        shoppingBrandGoodsMapper.updateBrandShowStateById(brandGoodsDTO);
    }

}
